<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class KcCount extends BaseController
{
    protected $title='库存汇总表';
    protected $table='stock_order_detail';
    private $sessionKey='pageKcCount';
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['sod.status','=',2];
            $where[]=['sod.type','in',['2','3']];
//            foreach (['bar_code','mdName','specs','brand'] as $i){
//                if (isset($param[$i])&&$param[$i]){
//                    $where[]=[$i,'like',"%".$param[$i].'%'];
//                }
//            }
            if (isset($param['brand'])&&$param['brand']){
                $where[]=['md.brand','like',"%".$param['brand'].'%'];
            }
            if (isset($param['mdName'])&&$param['mdName']){
                $where[]=['md.name','like',"%".$param['mdName'].'%'];
            }
            if (isset($param['specs'])&&$param['specs']){
                $where[]=['md.specs','like',"%".$param['specs'].'%'];
            }
            if (isset($param['buyCode'])&&$param['buyCode']){
                $where[]=['md.buyCode','like',"%".$param['buyCode'].'%'];
            }
            if (isset($param['taxCode'])&&$param['taxCode']){
                $where[]=['md.taxCode','like',"%".$param['taxCode'].'%'];
            }
            if (isset($param['cgrkd'])&&$param['cgrkd']){
                $cgMxId=Db::name($this->table)
                    ->where('type',3)
                    ->where('status',2)
                    ->where('stokcOrderNum','like','%'.$param['cgrkd'].'%')
                    ->column('saleContractDetailId');
                $xsmxId=Db::name('sale_contract_detail')
                    ->where('status',4)
                    ->where('type',2)
                    ->where('id','in',$cgMxId)
                    ->column('saleContractDetailId');
                $where[]=['saleContractDetailId','in',$xsmxId];
            }
//            halt($where);
            if (isset($param['cght'])&&$param['cght']){
                $where[]=['sc.orderNum','like',"%".$param['cght'].'%'];
            }
            if (isset($param['xsHt'])&&$param['xsHt']){
                $where[]=['scd.saleContractOrderNum','like',"%".$param['xsHt'].'%'];
            }

            if (isset($param['pName'])&&$param['pName']){
                $where[]=['p.name','like',"%".$param['pName'].'%'];
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias(' sod')
                ->leftJoin('material_dossier md','md.id=sod.mdId')
                ->where($where)
                ->group('sod.mdId')
                ->field("sod.*,
                md.name as mdName, md.brand,md.specs,md.buyCode,md.taxCode,md.unit")
                ->paginate($rows)->each(function ($item,$index){
                    $rukuCount=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('mdId',$item['mdId'])
                        ->where('type','in',[2,3])
                        ->sum('num');
                    $chukuCount=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('mdId',$item['mdId'])
                        ->where('type','in',[1,4])
                        ->sum('num');
                    $kcNum=$rukuCount-$chukuCount;
                    $lockNum=Db::name('sale_contract_lock')
                        ->where('status',1)
                        ->where('mdId',$item['mdId'])
                        ->sum('lockNum');
                    $canUse=$kcNum-$lockNum;
                    $kcMony=$this->getKcMoney($item['mdId']);
                    $kcPrice=$kcMony/$rukuCount;
                    $item['kcNum']=$rukuCount;
                    $item['canUse']=$canUse;
                    $item['kcPrice']=round($kcPrice,2);
                    $item['kcMoney']=$kcMony;
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            $warehouse=Db::name('warehouse')->where('status',1)->field('id,name')->select();
            View::assign('warehouseList',$warehouse);
            return view();
        }

    }


    public function getKcMoney($mdId)
    {
        $list=Db::name('stock_order_detail')
            ->alias('sod')
            ->leftJoin('sale_contract_detail scd','scd.id=sod.saleContractDetailId')
            ->where('sod.type','in',[2,3])
            ->where('sod.status','=',2)
            ->where('sod.mdId',$mdId)
            ->field('sod.num,scd.bjPrice')
            ->select();
        $money=0;
        foreach ($list as $v){
            $price=$v['num']*$v['bjPrice'];
            $money+=round($price,2);
        }
return $money;
    }
}